var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navTab: ['领取补贴', '已领取', '审核失败'],
    currentTab: 0,
    sendList: [],
    list: [],
    page: 1,
    size: 6,
    isEnd: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTopCatList()
  },
  getTopCatList() {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=PddOrder&a=getOrderList',
      data: {
        token: app.globalData.token,
        type: "1",
        free_status:this.data.currentTab+1,
        is_free: "Y",
        p: this.data.page,
        per: 10
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {

        console.log('ppp');
        wx.hideLoading();

        if (res.data.code == 0) {
          for(var i=0;i<res.data.data.list.length;i++){
            res.data.data.list[i].after=that.hh(res.data.data.list[i].order_pay_time*1000,'Y-M-D h:m')
          }
          if (that.data.page == 1) {
            that.setData({
              list: []
            })
          }
          that.setData({
            list: (that.data.list).concat(res.data.data.list)
          })
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none'
          })

        }
      },
      fail: function () {
        wx.hideLoading();
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:1
    })
    this.getTopCatList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
/**
 * 时间戳转化为年 月 日 时 分 秒
 * ts: 传入时间戳
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致
*/

formatNumber:function(n){
  n = n.toString()
  return n[1] ? n : '0' + n
},
 hh:function(timestamp, format) {
  console.log(timestamp)

    const formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    let returnArr = [];

    let date = new Date(timestamp);
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()
    returnArr.push(year, month, day, hour, minute, second);

    returnArr = returnArr.map(this.formatNumber);

    for (var i in returnArr) {
        format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;

},
  linqu:function(e){
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=PddOrder&a=getOrderList',
      data: {
        token: app.globalData.token,
        type: "1",
        order_status:this.data.currentTab ==0?"0":this.data.currentTab ==1?"2":this.data.currentTab ==2?"":"4",
        is_free: "Y",
        p: 1,
        per: 10
      },
      header: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {

        console.log('ppp');
        wx.hideLoading();

        if (res.data.code ==10) {
          var priceQuan;
          for(var i=0;i<res.data.data.list.length;i++){
            res.data.data.list[i].after=that.hh(res.data.data.list[i].order_pay_time*1000,'Y-M-D h:m')
          }
          if (that.data.page == 1) {
            that.setData({
              list: []
            })
          }
          that.setData({
            list: (that.data.list).concat(res.data.data.list)
          })
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none'
          })

        }
      },
      fail: function () {
        wx.hideLoading();
      }
    });
  },
  copyDh: function (e) {
    wx.setClipboardData({
      data: this.data.list[e.currentTarget.dataset.index].order_sn,
      success: function (res) {}
    })
  },
  upload: function (order_sn, url) {
    var that = this;
    wx.showLoading({
      title: '上传中',
      mask: false
    })
    wx.uploadFile({
      filePath: url,
      name: 'img',
      url: app.globalData.api + '/app.php?c=PddOrder&a=confirm',
      formData: { //需要的参数
        token: app.globalData.token,
        order_sn: order_sn
      }, // HTTP 请求中其他额外的 form data
      header: {
        "Content-Type": "multipart/form-data"
      },
      success: function (res) {
        console.log(res)
        var data = JSON.parse(res.data);
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        if (data.code == 0) {
          that.setData({
            page:1
          })
          that.getTopCatList()
        }
      },
      fail: function (res) {
        console.log(res)
        wx.showToast({
          title: '上传失败',
          icon: 'none'
        })
      },
      complete(res) {
        wx.hideLoading();
      }

    })
  },
  upImg: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        that.upload(that.data.list[e.currentTarget.dataset.index].order_sn, tempFilePaths[0])
      }
    })
  },
  currentTab: function (e) {
    if (this.data.currentTab == e.currentTarget.dataset.idx) {
      return;
    }
    this.setData({
      currentTab: e.currentTarget.dataset.idx,
      page: 1,
      list:[]
    })

    this.getTopCatList()
  },
  onReachBottom: function () {
    this.setData({
      page: ++this.data.page 
    })
    this.getTopCatList()
  },
  getData: function () {

  },
})